import React from 'react';
import { RequestConfig } from 'umi';

const baseUrl = "https://api.blog.wipi.tech/api";
const Authorization='Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjMxN2EyYWM4LTc1ZmItNGZiNS05N2ExLWI2MzVmYjdlM2ViNiIsIm5hbWUiOiJqYXNvbiIsImVtYWlsIjpudWxsLCJyb2xlIjoidmlzaXRvciIsImlhdCI6MTYyNTA1ODk3OCwiZXhwIjoxNjI1MDczMzc4fQ.5tgWleNtvqsmoR099c0gnHY_QgM7a-Jj135JwHFLeSg'
export const request: RequestConfig = {
  timeout: 100000,
  errorConfig: {},
  middlewares: [],
  // 请求拦截器
  requestInterceptors: [(url, options) => {
    let headers = { ...options.headers, Authorization }
    return {
      url: baseUrl + url,
      options: { ...options ,headers},
    };
  }],
  // 响应拦截器
  responseInterceptors: [async (response, options) => {
    let data = { code: 200, msg: '' };
    try {
      data = await response.clone().json();
      // 处理网络错误
      if (response.status !== 200) {
        return response;
      }
      // 处理业务错误
      if (data.code == 401) {
        console.log('业务有错')
      } else if (data.code !== 200) {
        return response;
      }
    } catch (e) {
      return response;
    }
    return response;
  }],
};
